package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.CommunityPublishTopicPicAdapter;
import com.rjwh.dingdong.client.adapter.CommunityPublishTopicPicAdapter.OnItemClickListener;

import java.util.List;

/**
 * 社区发帖--段落控件
 *
 * @author 王小珂
 * @ClassName PublishTopicParagraphLayout
 * @Description 社区发帖--Item控件
 * @date 2016-4-25 上午11:38:25
 * @history 1.YYYY-MM-DD author: description:
 */
public class PublishTopicParagraphLayout extends LinearLayout {
    private onParagraphListener listener;
    private EditText editTextView;
    private CommunityPublishTopicPicAdapter mAdapter;

    public PublishTopicParagraphLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PublishTopicParagraphLayout(Context context) {
        super(context);
        init();
    }

    public void setonParagraphListener(onParagraphListener listener) {
        this.listener = listener;
    }

    public void init() {
        final View view = inflate(getContext(),
                R.layout.community_post_publish_paragraph, null);
        LayoutParams ps = new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        ps.setMargins(20, 20, 20, 30);
        addView(view, ps);
        RecyclerView picRecyclerView = (RecyclerView) view
                .findViewById(R.id.act_community_post_publish_paragraph_pic_rv);
        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(
                getContext());
        // 设置横向滑动
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        picRecyclerView.setLayoutManager(linearLayoutManager);
        // 设置item间隔
        int spacingInPixels = getResources().getDimensionPixelSize(
                R.dimen.time_margin_bottom);
        picRecyclerView.addItemDecoration(new SpaceItemDecoration(
                spacingInPixels));
        mAdapter = new CommunityPublishTopicPicAdapter(getContext());
        picRecyclerView.setAdapter(mAdapter);
        // 设置header("+"按钮)
        View header = inflate(getContext(),
                R.layout.community_post_publish_paragraph_header, null);
        mAdapter.setHeaderView(header);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(int position, String data) {
                if (listener != null) {
                    listener.showImage(PublishTopicParagraphLayout.this,
                            position);
                }
            }
        });
        header.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.addImage(PublishTopicParagraphLayout.this);
                }
            }
        });
        editTextView = (EditText) view
                .findViewById(R.id.act_community_post_publish_paragraph_edit_et);
        ImageView delBtn = (ImageView) view
                .findViewById(R.id.act_community_post_publish_paragraph_del_tv);

        delBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.delItemView(PublishTopicParagraphLayout.this);
                }
            }
        });
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, int itemPosition,
                                   RecyclerView parent) {
            outRect.set(0, 0, space, 0);
        }
    }

    /**
     * 传入图片集合
     *
     * @param @param listImage
     * @return void
     * @throws
     * @Title: addImageList
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public void addImageList(List<String> listImage) {
        mAdapter.addDatas(listImage);
    }

    /**
     * 删除图片
     *
     * @param @param listImage
     * @return void
     * @throws
     * @Title: delImageList
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @history 1.YYYY-MM-DD author: description:
     */
    public void delImageList(List<String> listImage) {
        mAdapter.clear();
        mAdapter.addDatas(listImage);
    }

    public int getImageListCount() {
        return mAdapter.getItemCount() - 1;// 除去"+"按钮所占位置
    }

    ;

    public List<String> getImageList() {
        return mAdapter.getPicList();
    }

    public void setEditText(String key) {
        if (editTextView.isFocused()) {
            if (editTextView != null && !TextUtils.isEmpty(key)) {
                int start = editTextView.getSelectionStart();
                CharSequence content = editTextView.getText()
                        .insert(start, key);
                editTextView.setText(content);
                // 定位光标位置
                CharSequence info = editTextView.getText();
                if (info instanceof Spannable) {
                    Spannable spanText = (Spannable) info;
                    Selection.setSelection(spanText, start + key.length());
                }
            }
        }
    }

    public String getText() {
        String textString = editTextView.getText().toString();
        if (!AsStrUtils.isEmpty(textString)) {
            return textString;
        }
        return "";
    }

    public interface onParagraphListener {
        /**
         * 删除段落
         *
         * @param @param view
         * @return void
         * @throws
         * @Title: delItemView
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void delItemView(PublishTopicParagraphLayout view);

        /**
         * 添加图片
         *
         * @param @param view
         * @return void
         * @throws
         * @Title: addImage
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void addImage(PublishTopicParagraphLayout view);

        /**
         * 进入大图
         *
         * @param @param view
         * @param @param position
         * @return void
         * @throws
         * @Title: showImage
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void showImage(PublishTopicParagraphLayout view, int position);

        /**
         * 处理EditText
         *
         * @param @param view
         * @return void
         * @throws
         * @Title: editInput
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void editInput(PublishTopicParagraphLayout view);

    }

}
